package com.power.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.power.constant.BusinessEnum;
import com.power.model.Result;
import com.power.domain.SysRole;
import com.power.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/sys/role")
public class SysRoleController {
    @Autowired
    private SysRoleService service;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @RequestMapping("/list")
    @PreAuthorize("hasAnyAuthority('sys:role:list')")//权限认证
    public Result getList(){
        List<SysRole> list = service.getlist();
        return Result.success(list);
    }
    @GetMapping("/page")
    public Result page(@RequestParam("current") Integer current, @RequestParam("size")Integer size,@RequestParam(required = false ,value="roleName") String name){
        Page<SysRole> page=new Page<>(current,size);
       Page pageList= service.getPageList(page,name);
       return  Result.success(pageList);
    }
    @PostMapping
    public Result save(@RequestBody SysRole sysRole){
        boolean save = service.saveSysRole(sysRole);
        return Result.success(save);
    }
    @GetMapping("/info/{id}")
    @PreAuthorize("hasAnyAuthority('sys:role:info')")
    public Result getInfoById(@PathVariable Long id)
    {
        SysRole sysRole=service.getInfoById(id);
        return Result.success(sysRole);
    }
    @PutMapping
    public Result UpdateById(@RequestBody SysRole sysRole)
    {
        boolean b=service.updateByIdMenu(sysRole);
        return b? Result.success("修改成功"):Result.fail(BusinessEnum.OPERATION_FAIL);
    }
    @DeleteMapping
    public Result deleteById(@RequestBody List<Long> ids)
    {
        boolean b=service.removeBatchByIdsMenu(ids);
        return b? Result.success("删除成功"):Result.fail(BusinessEnum.OPERATION_FAIL);
    }
}
